#!/bin/bash

# =============================================================================
# Fedora Workstation 42 自动化配置脚本
# 功能：系统更新、Flatpak 配置、GNOME 扩展安装、主题美化等
# cd ~/下载 && git clone https://gitee.com/lcqh2635/init-fedora.git && cd init-fedora
# 使用方法：chmod +x fedora-install-gnome-extensions.sh && ./fedora-install-gnome-extensions.sh
# 注意：请在全新安装的 Fedora 系统上运行
# 脚本存放位置：阿里网盘网页版 https://www.aliyundrive.com
# =============================================================================

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印彩色信息函数
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查是否以 root 权限运行（部分操作需要）
check_root() {
    if [ "$EUID" -eq 0 ]; then
        print_error "请不要以 root 用户运行此脚本！"
        print_info "脚本会自动使用 sudo 执行需要管理员权限的操作。"
        exit 1
    fi
}

# 更新系统
update_system() {
    print_info "正在更新系统软件包..."
    sudo dnf upgrade -y
    if [ $? -eq 0 ]; then
        print_success "系统更新完成！"
    else
        print_error "系统更新失败！"
    fi
}

# 安装常用软件包
install_common_packages() {
    print_info "正在安装常用软件包..."
    
    cd ~/下载
    # 基础开发工具
    sudo dnf install -y git curl wget vim htop zip unzip p7zip nodejs make cmake just meson glib2-devel sassc fastfetch wl-clipboard
    # development-tools 是一个预定义的软件包组，包含一组常用的开发工具和库，用于支持软件开发工作。例如：git
    # c-development 是简化C开发环境配置的包组，安装后即可获得编译、调试和构建C程序所需的核心工具。如果你需要开发C程序，安装它或对应的包组是第一步。例如：gcc、gcc-c++
    # rpm-development-tools	是专门用于 RPM 包开发 的工具集，适合软件打包、维护或发布 RPM 格式的软件。例如：rpm-build、rpmdevtools
    # multimedia 包组提供了一套完整的音视频处理工具链，适合普通用户或开发者处理多媒体任务。例如：gstreamer1-plugin-*
    sudo dnf group install -y development-tools c-development rpm-development-tools multimedia
    # dnf group install 旨在为开发者提供一个基础的开发环境，而无需手动安装每个工具。
    # dnf group list			# 查看可用的软件包组
    # dnf group info development-tools	# 查看软件包组的信息
    # dnf group info c-development	# 查看软件包组的信息
    # dnf group info multimedia		# 查看软件包组的信息
    
    # 最新地址 淘宝 NPM 镜像站喊你切换新域名啦!
    npm config set registry https://registry.npmmirror.com
    sudo npm install -g typescript
    
    # 其他实用工具
    sudo dnf install -y gnome-tweaks gnome-extensions-app dconf-editor
    sudo dnf install -y evolution obs-studio
    flatpak install flathub com.mattjakeman.ExtensionManager -y
    
    if [ $? -eq 0 ]; then
        print_success "常用软件包安装完成！"
    else
        print_error "软件包安装失败！"
    fi
}

# gnome-extensions list
# dnf list gnome-shell-extension*
# 安装 GNOME 扩展
install_gnome_extensions() {
    print_info "卸载自带无用  GNOME 扩展..."
    sudo dnf remove -y \
    gnome-shell-extension-apps-menu \
    gnome-shell-extension-places-menu \
    gnome-shell-extension-window-list \
    gnome-shell-extension-launch-new-instance

    print_info "正在安装 GNOME 扩展..."
    # User Themes - 允许使用自定义主题
    sudo dnf install -y gnome-shell-extension-user-theme
    # Dash to Dock - 可停靠的 Dash：将应用启动器变为可停靠的侧边栏（经典 Dock）
    sudo dnf install -y gnome-shell-extension-dash-to-dock
    # ls /usr/share/gnome-shell/extensions
    # nautilus /usr/share/gnome-shell/extensions/blur-my-shell@aunetx
    # Blur My Shell - 毛玻璃效果：为顶部栏、Dash、应用菜单等添加背景模糊
    sudo dnf install -y gnome-shell-extension-blur-my-shell
    # Just Perfection - 精细控制 GNOME Shell 各组件的显示/隐藏（高度可定制）
    # No Overview 的更现代的替代方案：使用 Just Perfection 扩展，
    # 可选择性禁用 Overview 的某些元素（如工作区缩略图），而不完全破坏导航逻辑。
    sudo dnf install -y gnome-shell-extension-just-perfection
    # Removable Drive Menu 在顶部栏添加一个磁盘/挂载设备的快捷菜单，方便弹出 USB、挂载分区等
    sudo dnf install -y gnome-shell-extension-drive-menu
    # AppIndicator - 系统托盘图标支持：显示传统应用的托盘图标（如 Telegram、网易云音乐等）
    sudo dnf install -y gnome-shell-extension-appindicator
    # Caffeine - 防休眠开关：临时禁用自动锁屏/休眠，适合演示或视频播放
    sudo dnf install -y gnome-shell-extension-caffeine
    # Auto Move Windows - 自动将指定应用窗口分配到指定工作区（如终端→工作区2，浏览器→工作区1）
    sudo dnf install -y gnome-shell-extension-auto-move-windows
    # Forge - 窗口平铺
    sudo dnf install -y gnome-shell-extension-forge
    # Workspace Indicator - 在顶部栏显示当前工作区编号或名称
    sudo dnf install -y gnome-shell-extension-workspace-indicator
    # No Overview
    sudo dnf install -y gnome-shell-extension-no-overview
    
    # Github 克隆加速网站 https://gh-proxy.com/
    # 用户级 GNOME 扩展
    # nautilus ~/.local/share/gnome-shell/extensions
    
    
    cd ~/下载
    git clone https://gh-proxy.com/https://github.com/fthx/appmenu-is-back.git
    # zip -r 压缩包名.zip 文件夹名/
    zip -FSr appmenu-is-back.zip appmenu-is-back/*
    gnome-extensions install -f appmenu-is-back.zip
    cd ~/下载
    rm -rf appmenu-is-back*
    
    cd ~/下载
    git clone https://gh-proxy.com/https://github.com/Tommimon/add-to-desktop.git
    cd add-to-desktop
    ./build.sh
    gnome-extensions install -f output/add-to-desktop@tommimon.github.com.v15.shell-extension.zip
    cd ~/下载
    rm -rf add-to-desktop
    
    cd ~/下载
    git clone https://gh-proxy.com/https://github.com/maniacx/Bluetooth-Battery-Meter.git
    cd Bluetooth-Battery-Meter
    ./install.sh
    cd ~/下载
    rm -rf Bluetooth-Battery-Meter
    
    cd ~/下载
    git clone https://gh-proxy.com/https://github.com/Tudmotu/gnome-shell-extension-clipboard-indicator.git
    cd gnome-shell-extension-clipboard-indicator
    make bundle
    gnome-extensions install -f bundle.zip
    cd ~/下载
    rm -rf gnome-shell-extension-clipboard-indicator
    
    cd ~/下载
    git clone https://gh-proxy.com/https://github.com/hermes83/compiz-alike-magic-lamp-effect.git
    cd compiz-alike-magic-lamp-effect
    ./zip.sh
    gnome-extensions install -f compiz-alike-magic-lamp-effect@hermes83.github.com.zip
    cd ~/下载
    rm -rf compiz-alike-magic-lamp-effect
    
    cd ~/下载
    git clone https://gh-proxy.com/https://github.com/dsheeler/CoverflowAltTab.git
    cd CoverflowAltTab
    make all
    cd ~/下载
    rm -rf CoverflowAltTab
    
    cd ~/下载
    # https://github.com/ddterm/gnome-shell-extension-ddterm/blob/master/docs/Install.md
    wget https://ddterm.github.io/gnome-shell-extension-ddterm/ddterm@amezin.github.com.shell-extension.zip
    gnome-extensions install -f ddterm@amezin.github.com.shell-extension.zip
    cd ~/下载
    rm -rf ddterm@amezin.github.com.shell-extension.zip
    
    cd ~/下载
    # Disable Unredirect - 禁用 X11/Wayland 合成器中的“取消重定向”优化机制，
    # 目的是解决某些全屏应用中的画面撕裂或卡顿问题。解决 Hide Top Bar 弹跳 BUG
    git clone https://gh-proxy.com/https://github.com/Exeos/disable-unredirect.git
    cd disable-unredirect
    make install
    cd ~/下载
    rm -rf disable-unredirect
    
    cd ~/下载
    git clone https://gitlab.com/smedius/desktop-icons-ng.git
    cd desktop-icons-ng
    ./scripts/local_install.sh
    cd ~/下载
    rm -rf desktop-icons-ng
    
    cd ~/下载
    # https://github.com/tuxor1337/hidetopbar
    # Hide Top Bar - 自动隐藏顶部栏，最大化屏幕利用率（尤其适合小屏笔记本）
    git clone https://gh-proxy.com/https://github.com/tuxor1337/hidetopbar.git
    cd hidetopbar
    make
    gnome-extensions install -f hidetopbar.zip
    cd ~/下载
    rm -rf hidetopbar
    
    cd ~/下载
    # https://github.com/tuberry/ibus-tweaker
    # Ibus Tweaker - GNOME Shell 扩展，用于调整 IBus 字体、输入模式、剪贴板历史记录等。 
    git clone https://gh-proxy.com/https://github.com/tuberry/ibus-tweaker.git
    cd ibus-tweaker
    meson setup build && meson install -C build
    cd ~/下载
    rm -rf ibus-tweaker
    
    cd ~/下载
    git clone https://gh-proxy.com/https://github.com/Aryan20/Logomenu.git
    cd Logomenu
    make install
    cd ~/下载
    rm -rf Logomenu
    
    cd ~/下载
    git clone https://gitlab.com/rmnvgr/nightthemeswitcher-gnome-shell-extension.git
    cd nightthemeswitcher-gnome-shell-extension
    # 用户级别安装
    meson setup builddir --prefix=~/.local && meson install -C builddir
    cd ~/下载
    rm -rf nightthemeswitcher-gnome-shell-extension

    cd ~/下载
    git clone https://gh-proxy.com/https://github.com/qwreey/quick-settings-tweaks.git
    cd quick-settings-tweaks
    ./install.sh build && ./install.sh install
    cd ~/下载
    rm -rf quick-settings-tweaks
    
    cd ~/下载
    git clone https://gh-proxy.com/https://github.com/lennart-k/gnome-rounded-corners.git
    cd gnome-rounded-corners
    make
    gnome-extensions install -f Rounded_Corners@lennart-k.zip
    cd ~/下载
    rm -rf gnome-rounded-corners
    
    cd ~/下载
    # https://github.com/flexagoon/rounded-window-corners
    # Rounded Window Corners Reborn - 为所有窗口添加圆角效果（Fedora 有些窗口是直角，此扩展可实现 macOS 风格）
    git clone https://gh-proxy.com/https://github.com/flexagoon/rounded-window-corners.git
    cd rounded-window-corners
    just install
    cd ~/下载
    rm -rf rounded-window-corners
    
    cd ~/下载
    git clone https://gh-proxy.com/https://github.com/icedman/search-light.git
    cd search-light
    make
    cd ~/下载
    rm -rf search-light
    
    cd ~/下载
    git clone https://gh-proxy.com/https://github.com/amivaleo/Show-Desktop-Button.git
    mv Show-Desktop-Button show-desktop-button@amivaleo
    zip -r show-desktop-button@amivaleo.zip show-desktop-button@amivaleo
    gnome-extensions install -f show-desktop-button@amivaleo.zip
    cd ~/下载
    rm -rf show-desktop-button@amivaleo show-desktop-button@amivaleo.zip
    
    cd ~/下载
    git clone https://gitlab.com/p91paul/status-area-horizontal-spacing-gnome-shell-extension.git
    cd status-area-horizontal-spacing-gnome-shell-extension
    ./buildforupload.sh
    gnome-extensions install -f status-area-horizontal-spacing@mathematical.coffee.gmail.com.zip
    cd ~/下载
    rm -rf status-area-horizontal-spacing-gnome-shell-extension
    
    cd ~/下载
    git clone https://gitlab.gnome.org/june/top-bar-organizer.git
    cd status-area-horizontal-spacing-gnome-shell-extension
    ./buildforupload.sh
    gnome-extensions install -f status-area-horizontal-spacing@mathematical.coffee.gmail.com.zip
    cd ~/下载
    rm -rf status-area-horizontal-spacing-gnome-shell-extension
    
    # 其他推荐安装的 GNOME 扩展，推荐购买对 Battery Health Charging 兼容性好的电脑
    # Battery Health Charging
    # Logo Menu
    # Kiwi Menu
    # Kiwi is not Apple
    # In Picture
    # Lockscreen Extension
    print_success "GNOME 扩展安装完成！"
    
    
    # 提示用户重启 GNOME Shell 以应用扩展
    read -p "是否现在重启 GNOME Shell 以应用扩展？(y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        killall -3 gnome-shell
    fi
    print_info "GNOME 扩展已经安装完毕，需重启 GNOME Shell 才能生效..."
}

# 主函数
main() {
    print_info "开始 Fedora Workstation 42 自动化配置..."
    
    # 检查权限
    check_root
    
    # 执行配置步骤
    update_system
    install_common_packages
    install_gnome_extensions
    
    print_success "=========================================="
    print_success "Fedora 配置完成！"
    print_success "建议重启系统以应用所有更改。"
    print_success "=========================================="
}

# 执行主函数
main "$@"


# 将上述代码保存为 `fedora-install-gnome-extensions.sh`
# 赋予执行权限 chmod +x fedora-install-gnome-extensions.sh
# 运行脚本 ./fedora-install-gnome-extensions.sh

